package com.huanong.it.xwweibo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanong.it.xwcommon.entity.Comment;
import com.huanong.it.xwcommon.entity.Weibo;
import com.huanong.it.xwcommon.service.WeiboService;
import com.huanong.it.xwcommon.vo.Result;
import com.huanong.it.xwweibo.authority.common.utils.SecurityUtils;
import com.huanong.it.xwweibo.mapper.CommentMapper;
import com.huanong.it.xwweibo.mapper.WeiboMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import  com.huanong.it.xwcommon.service.UserService;

import java.util.Date;
import java.util.List;

@RestController("myWeiboServiceImpl")
public class WeiboServiceImpl implements WeiboService {
    @Autowired
    private WeiboMapper weiboMapper;
    @Autowired
    private CommentMapper commentMapper;


    @Override
    public Result<Page<Weibo>> showList(int cur, int size) {
        Page<Weibo> page = new Page<>(cur, size);
        Page<Weibo> weibos = weiboMapper.selectPage(page, null);

        getComments(weibos.getRecords());
        return new Result<Page<Weibo>>(200, weibos, "查询成功");
    }

    @Override
    public Result<Page<Weibo>> showListByUser(String userLoginname, int cur, int size) {
        Page<Weibo> page = new Page<>(cur, size);
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Weibo::getUserLoginName, userLoginname);
        Page<Weibo> weibos = weiboMapper.selectPage(page, queryWrapper);

        getComments(weibos.getRecords());

        return new Result<>(200, weibos, "查询成功");
    }

    @Override
    public Result addWeibo(String title, String content, String img) {
        Weibo weibo = new Weibo();
        weibo.setTitle(title);
        weibo.setContent(content);
        weibo.setImg(img);
        weibo.setCreateTime(new Date());
        weibo.setUserLoginName(SecurityUtils.getCurrentUser().getUser().getUsername());
        weiboMapper.insert(weibo);
        return new Result(200, weibo, "发布成功");
    }

    @Override
    public Result delWeibo(int wbId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Comment::getWbId, wbId);
        commentMapper.delete(queryWrapper);
        return new Result(200, null, "删除成功");
    }

    @Override
    public Result updateWeibo(int wbId, String title, String content, String img) {
        Weibo weibo = weiboMapper.selectById(wbId);
        weibo.setTitle(title);
        weibo.setContent(content);
        weibo.setImg(img);
        weiboMapper.updateById(weibo);
        return new Result(200, weibo, "修改成功");
    }

    private void getComments(List<Weibo> weibos) {
        for(int i=0;i<weibos.size();i++){
            Weibo weibo = weibos.get(i);

            QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Comment::getWbId, weibo.getId());
            List<Comment> comments = commentMapper.selectList(queryWrapper);

            weibo.setComments(comments);
        }
    }
}
